<?php
/**
 * Created by PhpStorm.
 * User: l
 * Date: 2016/7/21
 * Time: 12:41
 */
namespace app\models;

use Yii;
use yii\db\ActiveRecord;

class Orderform_commodity extends ActiveRecord{

    public  function  getOrderCommodities($order_id){

        $orderCommodities = Orderform_commodity::find()->where(['orderform_id' => $order_id])->asArray()->all();
        return $orderCommodities;


    }

    //后台该订单下商品列表数据
    public function getTableData($orderform_id_str){

        $orderform_id=intval($orderform_id_str);

        $rows = (new \yii\db\Query())
            ->from('hcps_orderform_commodity')
            ->where(['del_flag' => 1,'orderform_id'=>$orderform_id])
            ->all();

        for($i=0;$i<count($rows);$i++){

            $commodity = (new \yii\db\Query())->from('hcps_commodity')->where(['commodity_id' => $rows[$i]['commodity_id']])->one();

            $rows[$i]['commodity_pic_url']= $commodity['pic_url'];

            $rows[$i]['commodity_name']= $commodity['commodity_name'];

            $rows[$i]['price']= $commodity['price'];

            $rows[$i]['money_total']= $commodity['price']*$rows[$i]['commodity_num'];


            $attr_opt = (new \yii\db\Query())->select('attr_opt_name')->from('hcps_attr_opt')->where(['attr_opt_id' => json_decode($rows[$i]['opt_ids_json'])])->all();


            $str = "";

            for($j=0;$j<count($attr_opt);$j++){
                $str.= $attr_opt[$j]['attr_opt_name'].' ';
            }

            $rows[$i]['attr_opt_name'] = substr($str,0,-1);

            $rows[$i]['attr_opt']=$attr_opt;


        }

        return $rows;

    }
}